package controllers

import (
	"awesomeProject1/modules"
	"awesomeProject1/pkg/app"
	"awesomeProject1/pkg/e"
	"awesomeProject1/services"
	"encoding/json"

	"github.com/gin-gonic/gin"
	uuid "github.com/satori/go.uuid"
)

func GetInputList(c *gin.Context) {
	err, data := services.GetInputList()
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
	}

	app.OK(c, gin.H{"value": data}, "查询成功")
}

func UpInput(c *gin.Context) {
	a, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(a, &m)

	Ids := m["Id"]
	CarNos := m["CarNo"]
	InputCouts := m["InputCout"]
	InputTons := m["InputTon"]

	services.UpInput(modules.Input{
		Id:        Ids,
		CarNo:     CarNos,
		InputCout: InputCouts,
		InputTon:  InputTons})

}
func DelInput(c *gin.Context) {
	a, _ := c.GetRawData()
	var m map[string]string
	json.Unmarshal(a, &m)

	services.DelInput(m["ids"])
}

func AddInput(c *gin.Context) {
	a, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(a, &m)

	Ids := uuid.NewV4().String()
	CarNos := m["CarNo"]
	InputCouts := m["InputCout"]
	InputTons := m["InputTon"]
	data := modules.Input{
		Id:        Ids,
		CarNo:     CarNos,
		InputCout: InputCouts,
		InputTon:  InputTons}

	err := services.AddInput(data)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, gin.H{"value": data}, "添加成功！")

}

//出货----------------------------------------------------
func GetOutputList(c *gin.Context) {
	err, data := services.GetOutputList()
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
	}

	app.OK(c, gin.H{"value": data}, "查询成功")
}

func UpOutput(c *gin.Context) {
	a, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(a, &m)

	sId := m["Id"]
	sCarNo := m["CarNo"]
	sOutput_cout := m["Output_cout"]
	sOutput_ton := m["Output_ton"]
	sOrder_no := m["Order_no"]
	sChuchuan := m["Chuchuan"]
	sGet_no := m["Get_no"]
	sBus_no := m["Bus_no"]
	sCang_money := m["Cang_money"]
	sExist_ton := m["Exist_ton"]

	data := modules.Output{
		Id:          sId,
		CarNo:       sCarNo,
		Output_cout: sOutput_cout,
		Output_ton:  sOutput_ton,
		Order_no:    sOrder_no,
		Chuchuan:    sChuchuan,
		Get_no:      sGet_no,
		Bus_no:      sBus_no,
		Cang_money:  sCang_money,
		Exist_ton:   sExist_ton}
	services.UpOutput(data)

}
func DelOutput(c *gin.Context) {

	a, _ := c.GetRawData()
	var m map[string]string
	json.Unmarshal(a, &m)
	services.DelOutput(m["ids"])
}

func AddOutput(c *gin.Context) {
	a, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(a, &m)

	Ids := uuid.NewV4().String()

	sCarNo := m["CarNo"]
	sOutput_cout := m["Output_cout"]
	sOutput_ton := m["Output_ton"]
	sOrder_no := m["Order_no"]
	sChuchuan := m["Chuchuan"]
	sGet_no := m["Get_no"]
	sBus_no := m["Bus_no"]
	sCang_money := m["Cang_money"]
	sExist_ton := m["Exist_ton"]

	data := modules.Output{
		Id:          Ids,
		CarNo:       sCarNo,
		Output_cout: sOutput_cout,
		Output_ton:  sOutput_ton,
		Order_no:    sOrder_no,
		Chuchuan:    sChuchuan,
		Get_no:      sGet_no,
		Bus_no:      sBus_no,
		Cang_money:  sCang_money,
		Exist_ton:   sExist_ton}
	err := services.AddOutput(data)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, gin.H{"value": data}, "添加成功！")
}
